'''
Created on 24 Oct 2012

@author: quirino.brizi
'''
from org.codesketch.loadit.decorator import accepts, selfType, returns

class Server():
    '''
    LoadIT server configuration container
    '''
    
    @accepts(selfType, str, bool, str, str, int)
    def __init__(self, address, secure=False, username='', password='', port=80):
        self.__address = address
        self.__port = port
        self.__secure = secure
        self.__username = username
        self.__password = password
    
    def getAddress(self): return self.__address
    def getPort(self): return self.__port
    def getSecure(self): return self.__secure
    def getUsername(self): return self.__username
    def getPassword(self): return self.__password
    
    def getBaseUrl(self):
        return self.__address
        #return 'https://%s:%d' % (self.__address, self.__port) if self.__secure else 'http://%s:%d' % (self.__address, self.__port)
    def isBasicAuth(self): 
        return self.__username or self.__password

class Target():
    '''
    Target server information
    '''
#    @accepts(selfType, str, bool, str, str, int)
#    def __init__(self, address, secure=False, username='', password='', port=80): 
#        self.__address = address
#        self.__port = port
#        self.__secure = secure
#        self.__username = username
#        self.__password = password
#        
#    def getAddress(self): return self.__address
#    def getPort(self): return self.__port
#    def getSecure(self): return self.__secure
#    def getUsername(self): return self.__username
#    def getPassword(self): return self.__password
    def __init__(self, addresses):
        self.__addresses = addresses
    
    def getAddresses(self): return self.__addresses

class Address():
    def __init__(self, protocol, host, port, path, username="", password="", params={}):
        self.__protocol = protocol
        self.__host = host
        self.__port = port
        self.__path = path
        self.__username = username
        self.__password = password
        self.__params = params
    
    def getProtocol(self): return self.__protocol
    def getHost(self): return self.__host
    def getPort(self): return self.__port
    def getPath(self): return self.__path
    def getUsername(self): return self.__username
    def getPassword(self): return self.__password;
    def getParameters(self): return self.__params;
    
    @returns(str)
    def getBaseUrl(self): return '%s://%s:%d' % (self.__protocol, self.__host, self.__port)
    def isBasicAuth(self): return self.__username or self.__password

class Parameter():
    '''
    Optional Test configuration parameter
    '''
    
    @accepts(selfType, str, str)
    def __init__(self, name, value):
        self.__name = name
        self.__value = value
    
    def getName(self): return self.__name
    def getValue(self): return self.__value
    
class Test():
    '''
    Global test configuration
    '''
    
    @accepts(selfType, str, int, int, Target)
    def __init__(self, identifier, duration, sampling, target):
        '''
        Initialise the Test object
        @param identifier: the test identifier
        @param duration: the test duration
        @param sampling: the response sampling
        @param target: the test Target object
        '''
        self.__identifier = identifier
        self.__duration = duration
        self.__sampling = sampling
        self.__target = target
        
        
    def getIdentifier(self): 
        '''
        Return the test identifier
        '''
        return self.__identifier
    def getDuration(self): 
        '''
        Return the test duration in minutes
        '''
        return self.__duration
    def getSampling(self): 
        '''
        Return the test sampling
        '''
        return self.__sampling
    def getTarget(self): 
        '''
        return the test target
        '''
        return self.__target
    
class Script():
    '''
    Custom script configuration container
    '''
    
    @accepts(selfType, str, list)
    def __init__(self, clazz, params=[]):
        self.__clazz = clazz
        self.__parameters = {}
        for param in params : self.__parameters[param.getName()] = param.getValue()
    
    def getClass(self): return self.__clazz
    @returns(dict)
    def getParameters(self): return self.__parameters
    @accepts(selfType, str)
    @returns(str)
    def getParameter(self, name): return self.__parameters.get(name)

class Configuration():
    '''
    Main configuration class, contains the execution information passed in by LoadIT server.
    '''

    @accepts(selfType, Server, Test, Script)
    def __init__(self, server, test, script):
        '''
        Create a new Configuration instance
        '''
        self.__test = test
        self.__server = server
        self.__script = script

    def getTest(self): return self.__test
    def getServer(self): return self.__server
    def getScript(self): return self.__script